﻿using FairyGUI;
using System;
using System.Collections;
using UnityEngine;

/// <summary>
/// 登录流程
/// </summary>
public class LoginProcedure : BaseProceduar
{

	UILoginWindow _ui;

	static LoginProcedure _inst;

	public static LoginProcedure inst {
		get {
			if (_inst == null) {
				_inst = new LoginProcedure ();
			}
			return _inst;
		}
	}

	public override void EnterProceduar (bool initUI)
	{
		DebugInfo.Log ("进入登录流程");

		// 初始化
		LocalStorage.ClearKey ("弹出公告");


		// 播放音效
		//AudioController.inst.PlayBGM (AudioController.LoadClip ("主背景音乐"));
		AudioController.inst.PlayBGM ("zhujiemian");

		// 打开连接
		if (!NetIOController.inst.ready) {
			NetIOController.inst.Connect (Constants.LOGIN_URL);
		}

		if (initUI) {
			InitUI ();
		}

		// 其他平台缓存登录信息，不需要每次都登录
#if !UNITY_STANDALONE_WIN && !UNITY_STANDALONE_OSX
        if (LoginReqInfo.HasCache())
        {
            LoginReqInfo reqInfo = LoginReqInfo.FromCache();
            if ((DateHelper.timestamp < reqInfo.timeout) 
                && reqInfo.valid)
            {
                DataKernel.inst.loginReqInfo = reqInfo;
            }
        }
#endif
	}

	public override void UpdateProceduar ()
	{
		base.UpdateProceduar ();
	}

	public override void LeaveProceduar ()
	{
		DebugInfo.Log ("退出登录流程");
	}

	protected override void HandleMessage (Message message)
	{
		switch (message.msgId) {
		case Commands.CMD_CTRL_BUTTON_CLICKED:
			switch (message.iParam) {
			case GameCommands.CMD_CTRL_LOGIN:
				HandleLoginCmd ();
				//EffectController.inst.PlayZhadan ();
				break;
			}
			break;

		default:
			base.HandleMessage (message);
			break;
		}
	}

	protected override bool ProcessPacket (IOMessage message)
	{
		switch (message.code) {
		case IOCmds.WS_CONN_OPEN:
                // 与服务器的连接打开
			UIRootController.inst.ShowMask (false);
			if (DataKernel.inst.loginReqInfo != null) {
				DoLogin (DataKernel.inst.loginReqInfo);
			}
			return true;

		default:
			return base.ProcessPacket (message);
		}
	}

	void InitUI ()
	{
		_ui = new UILoginWindow ();
		UIRootController.inst.SwitchTo (_ui);
        
#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX)
        _ui.hiddenOpenidInput();        
#endif

		DebugInfo.Log ("登陆界面初始化完毕");
	}

	void HandleLoginCmd ()
	{
		DebugInfo.Log ("开始登录");

		UIRootController.inst.ShowMask (true);

#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
		LoginReqInfo reqInfo = new LoginReqInfo ();
		reqInfo.openid = _ui.openid;
		reqInfo.name = _ui.openid;
		reqInfo.headpic = "";
		reqInfo.sex = 0;
		DoLogin (reqInfo);
#else
		WeChat.inst.Login(reqInfo => {
            DoLogin(reqInfo);
        });
#endif
	}

	void DoLogin (LoginReqInfo reqInfo)
	{
		UIRootController.inst.ShowMask (false);

		if (!reqInfo.valid) {
			DebugInfo.Error ("登录信息无效");
			return;
		}

		// 保存数据
//#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX)
//        reqInfo.SetCache();
//#endif

		DataKernel.inst.loginReqInfo = reqInfo;
		// 切换到主界面流程
		GameController.inst.SwitchTo (HomeProcedure.inst);
	}
}
